package com.hongdee.scm.allocationorder.controller;

import com.alibaba.fastjson.JSON;
import com.hongdee.base.entity.ResultEntity;
import com.hongdee.scm.allocationorder.service.ITScmAllocationOrderService;
import com.hongdee.scm.approvalflow.service.ITScmApprovalflowService;
import com.hongdee.scm.pushMessage.entity.TScmMapToJSON;
import com.hongdee.scm.pushMessage.service.ITScmPushMsgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Mr. Xu on 2017/5/15.
 */
@RestController
@RequestMapping("/scm/allocationOrder")
public class TScmAllocationOrderController {
    @Autowired
    private ITScmAllocationOrderService itScmAllocationOrderService;
    @Autowired
    private ITScmApprovalflowService itScmApprovalflowService;
    @Autowired
    private ITScmPushMsgService itScmPushMsgService;

    /**
     * 调拨分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/page")
    @ResponseBody
    public Map page(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itScmAllocationOrderService.page(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 调拨分页查询
     *
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/approvalPage")
    @ResponseBody
    public Map approvalPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {
        Map map = itScmAllocationOrderService.approvalPage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    /**
     * 调拨单保存
     *
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/save")
    @ResponseBody
    public ResultEntity save(@RequestParam(name = "uid", defaultValue = "0") String uid,
                             @RequestParam(name = "token", defaultValue = "") String token,
                             @RequestBody Map data) throws Exception {
        this.itScmAllocationOrderService.saveOrUpdate(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 修改调拨单
     * @param uid
     * @param token
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/saveAllocation")
    @ResponseBody
    public ResultEntity saveAllocation(@RequestParam(name = "uid", defaultValue = "0") String uid,
                                      @RequestParam(name = "token", defaultValue = "") String token,
                                      @RequestBody Map data) throws Exception {
        this.itScmAllocationOrderService.saveAllocation(data, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("保存成功！");
        res.setSuccess(true);
        return res;
    }



    /**
     * 根据调拨单id查询车辆信息
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/carList")
    @ResponseBody
    public ResultEntity carList(@RequestParam(name = "uid", defaultValue = "0") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestParam(name = "id", defaultValue = "") String id) throws Exception {
        List data = this.itScmAllocationOrderService.carList(id, uid);
        ResultEntity res = new ResultEntity();
        res.setMsg("查询成功！");
        res.setData(data);
        res.setSuccess(true);
        return res;
    }

    /**
     * 提交或审批
     *
     * @param uid
     * @param token
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping("/submitOrPass")
    @ResponseBody
    public ResultEntity submitOrPass(@RequestParam(name = "uid", defaultValue = "0") String uid,
                                     @RequestParam(name = "token", defaultValue = "") String token,
                                     @RequestParam(name = "id", defaultValue = "") String id,
                                     @RequestParam(name = "pid", defaultValue = "") String pid) throws Exception {

        Map map = this.itScmApprovalflowService.commitApproval(pid,id,uid);
        List saId = (ArrayList) map.get("saId");
        map.remove("saId");
        String title= TScmMapToJSON.obj2JsonString(map);
        //个推(返回审批明细)
        itScmPushMsgService.push(saId,title,"1");
        ResultEntity res = new ResultEntity();
        res.setMsg("提交成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     * 更新调拨出库打印次数
     *
     * @param uid
     * @param token
     * @param ids
     * @return
     * @throws Exception
     */
    @RequestMapping("/addTime")
    @ResponseBody
    public ResultEntity addTime(@RequestParam(name = "uid", defaultValue = "0") String uid,
                                @RequestParam(name = "token", defaultValue = "") String token,
                                @RequestBody String[] ids) throws Exception {
        this.itScmAllocationOrderService.addTime(ids);
        ResultEntity res = new ResultEntity();
        res.setMsg("打印成功！");
        res.setSuccess(true);
        return res;
    }

    /**
     *给财务的单独权限(查询该公司的所有调拨)
     * @param uid
     * @param token
     * @param start
     * @param limit
     * @param data
     * @return
     * @throws Exception
     */
    @RequestMapping("/pageFinance")
    @ResponseBody
    public Map pageFinance(@RequestParam(name = "uid", defaultValue = "") String uid,
                    @RequestParam(name = "token", defaultValue = "") String token,
                    @RequestParam(name = "start", defaultValue = "0") String start,
                    @RequestParam(name = "limit", defaultValue = "50") String limit,
                    @RequestBody Map data) throws Exception {

        Map map = itScmAllocationOrderService.pageFinance(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }

    @RequestMapping("/appApprovalPage")
    @ResponseBody
    public Map appApprovalPage(@RequestParam(name = "uid", defaultValue = "") String uid,
                            @RequestParam(name = "token", defaultValue = "") String token,
                            @RequestParam(name = "start", defaultValue = "0") String start,
                            @RequestParam(name = "limit", defaultValue = "50") String limit,
                            @RequestBody Map data) throws Exception {
        Map map = itScmAllocationOrderService.appApprovalPage(data, start, limit, uid);
        map.put("success", true);
        map.put("msg", "分页查询成功！");
        return map;
    }
}